package com.hanainfo.hanaApp;

import java.util.ArrayList;

import com.hanainfo.common.DrawBall;
import com.hanainfo.db.DataBaseHelper;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class myBall extends Activity{
	DataBaseHelper mDB = new DataBaseHelper(this);
	BallBean myBall;
	ArrayAdapter<BallBean> abc;
	ListView lv;
	ballAdpater adapter;
	private static final int Del_ID = Menu.FIRST + 3;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.myball);
	    
	    SQLiteDatabase db = mDB.getReadableDatabase();//dbconnection
	    
	    abc = new ArrayAdapter<BallBean>(getApplicationContext(), android.R.layout.activity_list_item);//배열
		lv = (ListView) findViewById(R.id.myBallList);//리스트뷰
	    
	    abc = mDB.myBall(this); //내 저장목록 불러오는 쿼리 실행
	    adapter = new ballAdpater(this, abc); //아답타와 연결해 리스트 구현
	    lv.setAdapter(adapter);
	    
	    lv.setCacheColorHint(Color.parseColor("#00000000")); //대충 뭐.. 탭했을때 색깔 변화 주기 위해 
	    lv.setDrawSelectorOnTop(false);
	    registerForContextMenu(lv);
	    lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				openContextMenu(arg0); // 롱클릭 말고 숏클릭시에도 컨텍스트 메뉴를 보여준다 ㅋ
			}
	    });
	    lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { //컨텍스트 메뉴 생성
			
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				// TODO Auto-generated method stub
				menu.add(0, 1, 0, "당첨확인"); //add(그룹id, menuId, 0 , 타이틀) 
				menu.add(0, 2, 0, "삭제");
				menu.add(0, 3, 0, "전체삭제");
				/*menu.add(0, 4, 0, Integer.toString(abc.getItem(v.getId()).Id));*/
				
			}
		});
	    db.close();

	}
	@Override
	public boolean onContextItemSelected(MenuItem item){ //컨텍스트 메뉴에서 셀렉트시 이벤트
		AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
		int position = menuInfo.position;
		String Id = Integer.toString(abc.getItem(position).Id);
		mDB = new DataBaseHelper(this); 
		SQLiteDatabase db = mDB.getWritableDatabase();
		Toast toast = new Toast(this);
		if(item.getItemId() == 3){  // 메뉴 ID값을 가져와 그에 해당하는 이벤트 실행
			db.execSQL("DELETE FROM TEST");
			toast = Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT);
		}else if(item.getItemId() == 2){ 
			// listview 와 arrayadapter를 썻을땐 
			//AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
			//이렇게 ID값을 찾는다.
			db.execSQL("DELETE FROM TEST WHERE ROWID='"+Id+"'"); 
			toast = Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT);
			Log.v("Id", Integer.toString(abc.getItem(menuInfo.position).Id));
		}else if(item.getItemId() == 1){
			BallBean cMyBall = new BallBean();
			BallBean cLastBall = new DataBaseHelper(this).lastBall();
			String FirstBall = abc.getItem(position).FirstBall;
			String SecondBall = abc.getItem(position).SecondBall;
			String ThirdBall = abc.getItem(position).ThirdBall;
			String FourthBall = abc.getItem(position).FourthBall;
			String FifthBall = abc.getItem(position).FifthBall;
			String SixthBall = abc.getItem(position).SixthBall;
			
			cMyBall.setFirstBall(FirstBall);
			cMyBall.setSecondBall(SecondBall);
			cMyBall.setThirdBall(ThirdBall);
			cMyBall.setFourthBall(FourthBall);
			cMyBall.setFifthBall(FifthBall);
			cMyBall.setSixthBall(SixthBall);
			
			String[] checkBall = new BallBean().check(cMyBall, cLastBall);
			String checked = "";
			for(int i=0 ; i<Integer.parseInt(checkBall[6]) ; i++){
				checked = checked + checkBall[i] + ", ";
			}
			if(checkBall[6].equals("0")){
				toast = Toast.makeText(getApplicationContext(), "꽝!" + checked, Toast.LENGTH_SHORT);
			}else if(Integer.parseInt(checkBall[6]) == 3){
				toast = Toast.makeText(getApplicationContext(), "5등 - 당첨번호 : " + checked, Toast.LENGTH_SHORT);
			}else{
				toast = Toast.makeText(getApplicationContext(), checkBall[6]+"개 - 당첨번호 : " + checked, Toast.LENGTH_SHORT);
			}
		}
		 
		adapter = new ballAdpater(this, abc); //이벤트 발생후 삭제가 있었다면 리스트뷰를 새로고침(refresh)
		abc.clear();
		abc = mDB.myBall(getApplicationContext());
		abc.notifyDataSetChanged();
		adapter = new ballAdpater(this, abc);
		lv = (ListView) findViewById(R.id.myBallList);
		lv.setAdapter(adapter);
		
		db.close();
		
		toast.show();
		
		return super.onContextItemSelected(item);
	}
	
	//리스트뷰를 만들기 위해 아탑타 연결
	class ballAdpater extends BaseAdapter{
		ArrayAdapter<BallBean> abc;
		LayoutInflater inflater;
		Context _context;
		public ballAdpater(Context context, ArrayAdapter<BallBean> ballbean){
			inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			abc = ballbean;
			_context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return abc.getCount();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return abc.getItem(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return abc.getItemId(position);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			int res = R.layout.myball;
			DrawBall draw = new DrawBall(_context);
			convertView = inflater.inflate(res, parent, false);
			//Log.d("position : ", Integer.toString(position));
			ImageView ball1 = (ImageView)convertView.findViewById(R.id.ball1);
			draw.setDrawble(ball1, abc.getItem(position).FirstBall);
			//Log.d("First", abc.getItem(position).FirstBall);
			ImageView ball2 = (ImageView)convertView.findViewById(R.id.ball2);
			draw.setDrawble(ball2, abc.getItem(position).SecondBall);
			//Log.d("Second", abc.getItem(position).SecondBall);
			ImageView ball3 = (ImageView)convertView.findViewById(R.id.ball3);
			draw.setDrawble(ball3, abc.getItem(position).ThirdBall);
			ImageView ball4 = (ImageView)convertView.findViewById(R.id.ball4);
			draw.setDrawble(ball4, abc.getItem(position).FourthBall);
			ImageView ball5 = (ImageView)convertView.findViewById(R.id.ball5);
			draw.setDrawble(ball5, abc.getItem(position).FifthBall);
			ImageView ball6 = (ImageView)convertView.findViewById(R.id.ball6);
			draw.setDrawble(ball6, abc.getItem(position).SixthBall);
			return convertView;
		}
	}
}
